package repo

import (
	"mine-info-server/domain/common-context/model/message-aggregate/aggregate"
	"mine-info-server/infra/database/mysql/common-context/message/convertor"
	"mine-info-server/infra/database/mysql/common-context/message/dao"
	"mine-info-server/interfaces/response"
)

/*
   @author:zhongyang
   @date:2023/4/12
   @description:消息-仓储层实现
*/

type MessageRepo struct {
	convertor convertor.MessageConvertor
}

func NewMessageRepo() *MessageRepo {
	return &MessageRepo{}
}

func (m *MessageRepo) FindMsgById(msgId string) (*aggregate.MessageAggregate, error) {
	msgDao := dao.NewRemindMsgDao()
	msgPO, err := msgDao.GetOne(msgId)
	if err != nil {
		return nil, err
	}
	return m.convertor.CreateAggregateByOption(convertor.WithRemindMsgPO(msgPO)), nil
}

func (m *MessageRepo) ReadMsg(msg *aggregate.MessageAggregate) error {
	msgPO := m.convertor.ReadMsgPO(msg)
	msgDao := dao.NewRemindMsgDao()
	return msgDao.ReadMsg(msgPO)
}

func (m *MessageRepo) IsMsgAllRead(userID string) (bool, error) {
	msgDao := dao.NewRemindMsgDao()
	return msgDao.IsMsgAllRead(userID)
}

func (m *MessageRepo) GetMsgPageByUserId(page int, userId string, pageSize int, isRead int) ([]*response.MsgItemResponse, int64, error) {
	msgDao := dao.NewRemindMsgDao()
	return msgDao.GetMsgPageByUserId(page, userId, pageSize, isRead)
}
